package com.tedu.pj.common.validator;

import com.tedu.pj.common.annotation.Validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/10/21
 */
public class Validator implements ConstraintValidator<Validation,Object> {
    private String contains;

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        //校验方法
        if (o == null) {
            return false;
        }
        if (o instanceof String) {
            String strMessage = (String) o;
            return strMessage.contains(contains);
        } else if (o instanceof Integer) {
            return contains.contains(String.valueOf(o));
        }
        return false;
    }

    @Override
    public void initialize(Validation constraintAnnotation) {
        contains = constraintAnnotation.contains();
    }
}
